import org.jmusixmatch.MusixMatch;
import org.jmusixmatch.entity.lyrics.Lyrics;
import org.jmusixmatch.entity.track.Track;
import org.jmusixmatch.entity.track.TrackData;


public class Song {

	public String title;
	public String artistName;
//	public String code;
	public String album;
	public String length;
	public boolean lyrics_found;
	public String lyrics;
	
	public Song(String title, String artistName) {
		this.title = title;
		this.artistName = artistName;
		album = "-";
		lyrics = "";
		length = "";
		lyrics_found = false;
		
	}
	
	public Song(String title, String artistName, String album, String length,
			boolean lyrics_found, String lyrics) {
		this.title = title;
		this.artistName = artistName;
		this.album = album;
		this.length = length;
		this.lyrics_found = lyrics_found;
		this.lyrics = lyrics;
	}


	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((artistName == null) ? 0 : artistName.hashCode());
		result = prime * result + ((title == null) ? 0 : title.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Song other = (Song) obj;
		if (artistName == null) {
			if (other.artistName != null)
				return false;
		} else if (!artistName.equals(other.artistName))
			return false;
		if (title == null) {
			if (other.title != null)
				return false;
		} else if (!title.equals(other.title))
			return false;
		return true;
	}

	// Song lyrics from MusixMatch
	public static void addSong(String song_title, String artist_name) {
		addSongThread thread = new addSongThread(song_title,artist_name);
		thread.start();	
	}
	
	public static class addSongThread extends Thread{
		
		String title, artist_name;
		String apiKey = "71dd2cc2de9e53d13ac03c9d783a7aa3"; // MusixMatch api
		
		public addSongThread(String song_title, String artist_name) {
			this.title = song_title;
			this.artist_name = artist_name;
		}
		
		public void run() {
			
			String album = "-";
			String lyrics_body = "";
			boolean lyrics_found = false;
			String length = "-";
			
			MusixMatch musixMatch = new MusixMatch(apiKey);
			
			// Track Search [ Fuzzy ]
			Track track;
			try {


				TrackData data;
				Lyrics lyrics;

				track = musixMatch.getMatchingTrack(title, artist_name);

				data = track.getTrack();

//				System.out.println("AlbumID : "    + data.getAlbumId());
//				System.out.println("Album Name : " + data.getAlbumName());
//				System.out.println("Artist ID : "  + data.getArtistId());
//				System.out.println("Album Name : " + data.getArtistName());
//				System.out.println("Track ID : "   + data.getTrackId());
				
				album = data.getAlbumName();

				int trackID = data.getTrackId();

				lyrics = musixMatch.getLyrics(trackID);
				lyrics_body = lyrics.getLyricsBody();
				
				Song s = new Song(title, artist_name, album, length, true, lyrics_body);
				GLOBAL.songsSelected.add(s);
				GLOBAL.removeSongsSelected.add(new CloseButtonSong(s));
			}
			catch(Exception e) {
				//System.err.println("MusixMatch lyrics download error");
				//e.printStackTrace();
				Song s = new Song(title, artist_name, album, length, false, lyrics_body);
				GLOBAL.songsSelected.add(s);
				GLOBAL.removeSongsSelected.add(new CloseButtonSong(s));
			}
			GLOBAL.songsArea.songsSelected.updateValues();
			GLOBAL.songsArea.changeTagCloud();
			GLOBAL.songsArea.tagCloud.updateTagCloud();
		}
	}
	
}
